<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>用户管理</title>
	<link type="text/css" rel="stylesheet" href="../<s:property value="#session.themeStyle.mainCss" default="Skin/PM/LightBlue/main.css"/>" />
</head>
<body>
	<div class="navBar">
		当前位置：系统管理&gt;&gt;用户管理
	</div>
	<s:form theme="simple">
	<fieldset class="query">
		<legend class="title">查询条件</legend>
		<ul style="margin-top: 0px;margin-left: 0px;">
			<li class="titles">
				组织机构：
			</li>
			<li class="inputs">
				<s:select id="deptId" name="deptId" list="managedOrgList" listKey="pkId" listValue="name" emptyOption="true" onchange="getData();"></s:select>
			</li>
			<s:if test="deptId==null">
			<s:hidden id="range" name="range" />
			</s:if><s:else>
			<li class="titles">
				范围：
			</li>
			<li class="inputs">
				<s:select id="range" name="range" list="rangeTypes" listKey="id" listValue="name" emptyOption="true" onchange="getData();"></s:select>
			</li>
			</s:else>
			<li class="titles">
				姓名：
			</li>
			<li class="inputs">
				<input class="inputText" name="name" id="name" type="text" value="${name}"  style="color: #5F9EA0;font-style: italic; "   onblur="changeBegin(this.id,'请输入查询内容')" onfocus="changeEnd(this.id,'请输入查询内容')"   />
			</li>
			<li class="titles">
				账号：
			</li>
			<li class="inputs">
				<input class="inputText" name="userName" id="userName" type="text" value="${userName}"  style="color: #5F9EA0;font-style: italic; "   onblur="changeBegin(this.id,'请输入查询内容')" onfocus="changeEnd(this.id,'请输入查询内容')"  />
			</li>
			<s:if test="mapResult['queryTypeList']!=null">
			<li class="titles">
				查询类型：
			</li>
			<li class="inputs">
				<s:select id="queryType" name="mapResult['queryType']" list="mapResult['queryTypeList']" listKey="id" listValue="name" onchange="getData();"></s:select>
			</li>
			</s:if>
			<li style="float: left; margin-left: 20px; margin-top: 0px;">
				<a class="but28" onclick="getData()" style="text-decoration: none;">查询</a>
				<a class="but28" onclick="addUser()" style="text-decoration:none;" >添加用户</a>
				
			</li>
		</ul>
	</fieldset>
	<input id="loginType" type="hidden" value="<s:property value="loginType"/>"/>
	<div class="tabBlock">
		<div class="dataGrid">
			<table style="overflow-y: auto;">
				<tr>
					<th style="width: 10%;">
						姓名
					</th>
					<th style="width: 10%;">
						账号
					</th>
					<th style="width: 30%;">
						部门/角色
					</th>
					<th style="width: 5%;">
						状态
					</th>
					<th style="width: 15%;">
						操作
					</th>
				</tr>
				<s:iterator value="result.resultlist" status="st" var="user">
					<tr class="trStyle">
						<td>
							<s:property value="name" />
							&nbsp;
						</td>
						<td>
							<s:property value="userName" />
							&nbsp;
						</td>
						<td>
							<s:property value="userDept" />
							<s:iterator value="roles" var="role">
								[<s:property value="name" />]&nbsp;
							</s:iterator>
						</td>
						<td>
							<s:if test="type==0">
								无效
							</s:if>
							<s:else>
								有效&nbsp;
							</s:else>
						</td>
						<td>
							<a href='javascript:void(0);' onclick='detail(<s:property value="pkId" />);return false;' style="color: #E9621E;">详情</a>&nbsp;
							<a href='javascript:void(0);' onclick="editUser(<s:property value="pkId" />);return false;" style="color: #e9621e;text-decoration: none;cursor: pointer;">修改</a>&nbsp;
							<span id="span_<s:property value="#st.index" />">
							<s:if test="loginType == 1">
								<s:if test="snCode > 0">
									<a href='javascript:void(0);' onclick='jcMK(<s:property value="pkId" />,<s:property value="snCode" />,<s:property value="#st.index" />,"<s:property value="name" />");return false;' style="color: green;">解除绑定</a>&nbsp;
								</s:if>
								<s:else>
									<a href='javascript:void(0);' onclick='bindMK(<s:property value="pkId" />,<s:property value="#st.index" />,"<s:property value="name" />");return false;' style="color: #E9621E;">绑定密令</a>&nbsp;
								</s:else>
								<a href='javascript:void(0);' onclick='delteUser(<s:property value="pkId" />,"<s:property value="name" />",<s:property value="snCode" />);return false;' style="color: #E9621E;">删除</a>&nbsp;
							</s:if>
							<s:else>
								<a href='javascript:void(0);' onclick='delteUser(<s:property value="pkId" />,"<s:property value="name" />",0);return false;' style="color: #E9621E;">删除</a>&nbsp;
								<a href='javascript:void(0);' onclick='reStorePWD(<s:property value="pkId" />,"<s:property value="userName" />");return false;' style="color: #E9621E;">密码重置</a>&nbsp;
							</s:else>
							</span>
						</td>
					</tr>
				</s:iterator>
				<tfoot class="trBotton">
					<tr>
						<td colspan="5">
							<jsp:include page="/page/page.jsp"></jsp:include>
						</td>
					</tr>
				</tfoot>
			</table>
		</div>
	</div>
	</s:form>
	<script type="text/javascript" src="../js/jquery.js"></script>
	<script type="text/javascript" src="../js/modalWindow.js"></script>
	<script type="text/javascript" src="../js/store.js"></script>
	<script type="text/javascript">
		$(document).ready(function() {
			var message = ${message}+"";
			if(message=="1"){
				alert("用户密码初始化成功！");
			}
			if($("#name").val()==""){
			$("#name").val("请输入查询内容");
			}
			if($("#userName").val()==""){
				$("#userName").val("请输入查询内容");
			}
		});
		
		function addUser(){
			var deptId = $("#deptId").val();
			if(deptId==0){
				alert("根部门不能添加用户！");
				return false;
			}
			var url = "userInfo-add.action?deptId="+deptId;
			showModalWindow(
					url,
					window,
					'resizable:no;scroll:yes;status:no;dialogWidth=600px;dialogHeight=450px;center=yes;help=no',
					function(result){
						if(typeof(result)!='undefined'&&result=="true"){
				   			window.location.reload();
						}
					}
			);
		}
		
		function saveUser(result){
		   	if(result!=null&&result!='undefined'&&result!=''){
				var deptId = $("#deptId").val();
		   		var info = eval(result);
		   		var from = createFrom("../userinfo/addUserInfo.action");
		   		loadFrom("userName",info[0].userName,from);
		   		loadFrom("name",info[0].name,from);
		   		loadFrom("password",info[0].password,from);
		   		loadFrom("telphone",info[0].telphone,from);
		   		loadFrom("email",info[0].email,from);
		   		loadFrom("address",info[0].address,from);
		   		loadFrom("deptId",deptId,from);
		   		from.submit();
			}
		}
		
		function delteUser(userId, name, sncode){
			var loginType = $("#loginType").val();
			if(loginType == 1 && sncode > 0){
				alert("用户["+name+"]已绑定密令卡，请先解除绑定后再删除！");
				return;
			}
			var tips = "你确定删除用户名叫（"+name+"）的用户？";
			if(confirm(tips)){
				$.ajax({
					url:"userInfo-deleteUser.action",
					type: "POST",
					data: {userId:userId},
					dataType: "json",
					success: function(result){
						if (result.success){
							alert("用户删除成功！");
							getData();
						}else{
							alert("用户删除失败！"+result.errorMessage);
						}
					}
				});
			}
		}
		
		function editUser(id){
			var deptId = $("#deptId").val();
			var url = "userInfo-edit.action?pkId="+id+"&deptInfo.pkId="+deptId;
			showModalWindow(
					url,
					window,
					'resizable:no;scroll:yes;status:no;dialogWidth=600px;dialogHeight=450px;center=yes;help=no',
					function(result){
						if(typeof(result)!='undefined'&&result=="true"){
				   			$("form:first")[0].submit();
						}
					}
			);
		}

		function resetPassword(userId,userName){
			var tips = "你确定重置账号为（"+userName+"）的用户密码为：【8888】？";
			if(confirm(tips)){
				//window.location = "reStoreUserPWD.action?userId="+userId;
				$.ajax({
					url: "userInfo-resetPassword.action",
					type: "POST",
					data: {userId: userId},
					dataType: "json",
					success: function(result){
						if (result.success){
							alert("用户密码已经重置");
						}else{
							alert("用户密码重置失败："+result.errorMessage);
						}
					}
				});

			}
		}

		function detail(userId){
			window.location = "userInfo-view.action?userId="+userId;
		}
		
		function reStorePWD(userId,userName){
			var tips = "你确定重置账号为（"+userName+"）的用户密码为：【8888】？";
			if(confirm(tips)){
				$.ajax({
					url: "userInfo-reStoreUserPWD.action",
					data: {userId: userId},
					dataType: "json",
					success: function(result){
						if(result.success){
							alert("密码已经重置！");
						}else{
							alert("密码重置失败！"+result.errorMessage);
						}
					}
				});
			}
		}
		
		function getData(){
			var page = $.trim($("#page").val());
			var rowsPage = $.trim($("#rowsPage").val());
			var userName = $.trim($("#userName").val());
			var name = $.trim($("#name").val());
			var deptId = $("#deptId").val();
			var orgId = $("#orgId").val();
			if (userName=="请输入查询内容"){
				userName="";
			}
			if (name=="请输入查询内容"){
				name="";
			}
			var form = $("form")[0];
			form.submit();
		}
		
		function changeBegin(cs_id,value){ 
			var str_text = document.getElementById(cs_id);
			if (str_text.value==""){
				str_text.value=value;
			}
		}
		
		function changeEnd(cs_id,value){ 
			var str_text = document.getElementById(cs_id);
			if (str_text.value==value){
				str_text.value="";
			}
		}
		
		//绑定密令卡
		function bindMK(id, index, name){
			var url = "../snTree/dynamicCipherCard-snInfoTree.action";
			//var result = window.showModalDialog(url,'','');
			showModalWindow(
					url,
					window,
					'resizable:no;scroll:yes;status:no;dialogWidth=300px;dialogHeight=450px;center=yes;help=no',
					function (result){
						if(result != null && result != 'undefined' && result != ''){
							var sn1 = result;
						    var url2 = "../snTree/dynamicCipherCard-updateSNInfo.action";
							$.ajax({
					            type: "POST",
					            url: url2,
					            data: {"snCode":sn1},
					            dataType: "json",
					            success: function (result) {
								 	var url3 = "../sys/userInfo-bindingUserMLInfo.action";
									$.ajax({
							            type: "POST",
							            url: url3,
							            data: {"pkId":id, "sn":sn1},
							            dataType: "json",
							            success: function (result) {
							            	if(result.success){
												alert("动态密令卡绑定成功！");
												var html = "<a href='javascript:void(0);' onclick='jcMK("+id+","+sn1+","+index+",\""+name+"\")' style=\"color: green;\">解除绑定</a>&nbsp;&nbsp;&nbsp;";
												html += "<a href='javascript:void(0);' onclick='delteUser("+id+",\""+name+"\","+sn1+")' style=\"color: #E9621E;\">删除</a>&nbsp;&nbsp;&nbsp;";
												$("#span_"+index).html(html);
							            	}else{
												alert("动态密令卡绑定失败:"+result.errorMessage);
							            	}
							            }
									});
					            }
							});
						}
					}

			);
			
		}
		//解除绑定密令卡
		function jcMK(pkId, snCode, index, name){
			if(confirm("是否确定解除绑定？")){
				if(snCode > 0){
					var url = "../snTree/jcSNInfo.action";
					$.ajax({
			            type: "POST",
			            url: url,
			            data: {"snCode":snCode},
			            dataType: "json",
			            success: function (result) {
			            	if(result.success){
				            	var url2 = "../userinfo/jcBinding.action";
								$.ajax({
						            type: "POST",
						            url: url2,
						            data: {"pkId":pkId},
						            dataType: "json",
						            success: function (result) {
						            	if(result.success){
											var html = "<a href='javascript:void(0);' onclick='bindMK("+pkId+","+index+",\""+name+"\")' target=\"mainFrame\" style=\"color: #E9621E;\">绑定密令</a>&nbsp;&nbsp;&nbsp;";
											html += "<a href='javascript:void(0);' onclick='delteUser("+pkId+",\""+name+"\",0)' target=\"mainFrame\" style=\"color: #E9621E;\">删除</a>&nbsp;&nbsp;&nbsp;";
											$("#span_"+index).html(html);
											alert("密令卡与用户解绑成功！");
						            	}else{
											alert("密令卡与用户解绑失败："+result.errorMessage);
						            	}
						            }
								});
			            	}else{
			            		alert("密令卡解绑失败："+result.errorMessage);
			            	}
			            }
					});
				}
			}
		}
	</script>
</body>
</html>